package com.gaoyi.basemodule.baseUtil;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.gaoyi.basemodule.R;

import java.util.ArrayList;
import java.util.Map;

public class PermissionUtils {

    private static final String[] mPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

    /**
     * 动态申请权限
     *
     * @param permissions 权限组
     * @return
     */
    public static boolean addPermission(FragmentActivity activity, String[] permissions, ActivityResultLauncher<String[]> resultLauncher) {
        boolean result = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissionList = new ArrayList<>();
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                    permissionList.add(permission);
                }
            }
            if (!permissionList.isEmpty()) {
                result = false;
                String[] permissionNew = permissionList.toArray(new String[0]);
                resultLauncher.launch(permissionNew);
            }
        }
        return result;
    }

    /**
     * 申请权限后回调判断用户是否同意权限
     *
     * @param result     返回内容
     * @param permission 要判断的权限
     */
    public static boolean requestPermissionResult(Map<String, Boolean> result, String permission) {
        Boolean granted = result.get(permission);
        return granted != null && granted;
    }

    public static boolean toCameraPermissionBack(FragmentActivity activity, Map<String, Boolean> result, ActivityResultLauncher<Intent> cameraResult) {
        if (PermissionUtils.requestPermissionResult(result, Manifest.permission.CAMERA)
                && PermissionUtils.requestPermissionResult(result, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            SystemUtils.getInstance().toCamera(activity, cameraResult);
            return true;
        }
        ToastUtil.showToast(R.string.p_allow_camera);
        return false;
    }

    public static boolean checkLocationPermission(FragmentActivity activity, ActivityResultLauncher<String[]> mPermissionsResult) {
        return addPermission(activity, mPermission, mPermissionsResult);
    }

    /**
     * 申請加入‘白名單’
     *
     * @param context
     */
    public static void addIgnoringBatteryOptimizations(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!isIgnoringBatteryOptimizations(context)) {
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(intent);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private static boolean isIgnoringBatteryOptimizations(Context context) {
        boolean isIgnoring = false;
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (powerManager != null) {
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
        }
        return isIgnoring;
    }
}
